<div id="formDiv"></div>
<script type="text/html" id="warehouseProductionOrderformContent">
  <form lay-filter="warehouseProductionOrderForm" id="warehouseProductionOrderForm" class="layui-form open-form">
    <div class="open-model">
      <div class="open-auto">
        <div>
          <table class="layui-table" id="topForm" lay-filter="topForm">
            <thead>
            <tr>
              <th lay-data="{field:'qualityTestingTypeId', width:150, sort: true, fixed: 'left'}">质检类型
              </th>
              <th lay-data="{field:'qualityTestingTypeId', width:150, sort: true, fixed: 'left'}">报工记录编号
              </th>
              <th lay-data="{field:'qualityTestingCategoryId', width:150, sort: true, fixed: 'left'}">
                质检分类
              </th>
              <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">所属生产订单</th>
<!--              <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">所属车间</th>-->
              <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">所属产线</th>
              <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">上机时间</th>
            </tr>
            </thead>
            <tbody>

            <tr>
              <td style="position: relative">
                <select id="qualityTestingTypeId" name="qualityTestingTypeId" lay-filter="qualityTestingTypeId"  lay-search required></select>
                {{# if(d.eventName == 'detail'){ }}
                <div class="mask" style="width: 100%;height: 100%;position: absolute;z-index: 10;left:0;top:0;background-color: rgba(0,0,0,0.001)"></div>
                {{# } }}
              </td>
              <td style="position: relative">
                {{# if(d.eventName != 'detail' && d.eventName !== 'processingReportAdd'){ }}
                <select id="processingReportId" name="processingReportId"
                        lay-filter="processingReportId"  lay-search required></select>
                        {{# } }}
                        {{# if(d.eventName == 'detail' || d.eventName == 'processingReportAdd'){ }}
                <input type="text" class="layui-input" id="processingReportCode" disabled name="processingReportCode"/>
                {{# } }}
              
              </td>
              <td style="position: relative">
                <select id="qualityTestingCategoryId" name="qualityTestingCategoryId" lay-filter="qualityTestingCategoryId"  lay-search required></select>
                {{# if(d.eventName == 'detail'){ }}
                <div class="mask" style="width: 100%;height: 100%;position: absolute;z-index: 10;left:0;top:0;background-color: rgba(0,0,0,0.001)"></div>
                {{# } }}
              </td>
              <td>
                <input type="text" class="layui-input" id="productionOrderNumber" disabled name="productionOrderNumber"/>
              </td>

<!--              <td>-->
<!--                <input type="text" class="layui-input" id="workshopName" disabled name="workshopName"/>-->
<!--              </td>-->
              <td>
                <input type="text" class="layui-input" id="productionLineName" disabled name="productionLineName"/>
              </td>
              <td>
                <input class="layui-input adv-disable" id="createTime" disabled name="createTime"/>
              </td>

            </tr>
            </tbody>
          </table>


          <div class="layui-form-item tableContent">
            <!--   {{# if(d.eventName !== 'detail'){ }} -->
<!--            <button class="layui-btn" type="button" id="Configure">添加记录</button>-->
            <!--                         {{# } }} -->
            <table class="layui-table" id="processPlanFormTable" lay-filter="processPlanFormTable"></table>
          </div>


          <script type="text/html" id="processPlanFormTableBar">
            {{# if(d.eventName !== 'detail'){ }}
            <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" type="button">删除</button>
            {{# } }}
</script>


<div class="layui-form-item text-right">
  <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">关闭</button>
  <button class="layui-btn" lay-filter="formSubmit" type="button" lay-submit id="formSubmitUp">提交</button>
</div>
</div>
</div>
</div>
</form>
</script>

<script type="text/html" id="warehouseMaterialRequisitionRecordFormTableBar">
  <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" type="button">删除</button>
</script>
<script type="text/html" id="customerSku">
  <div class="open-form">
    <div class="open-model">
      <div class="open-auto">
        <button class="layui-btn " id="addCustomerSku">确认选择</button>
        <table class="layui-table" id="customerSkuTable"></table>
      </div>
    </div>
  </div>
</script>
<script type="text/html" id="warehouseMaterialRequisitionRecordForm">
  <div class="open-form">
    <div class="open-model">
      <div class="open-auto">
        <table class="layui-table" id="warehouseMaterialRequisitionRecordTable"
               lay-filter="warehouseMaterialRequisitionRecordTable"></table>
      </div>
    </div>
  </div>
</script>
<script>
  // 假设使用一个简单的计数器来生成 productionuserBundleCodePlanCode
  let bundleCodeCounter = 1;
  let itemData = {}
  let oneItem = {}
  let selectData = {}
  let kData
  let practiceId = 0

  layui.use(["layer", "form", "table", "admin", "laydate", "upload", "laytpl"], async function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    let laydate = layui.laydate;
    let laytpl = layui.laytpl;
    const nowData = { ...parentData };
    console.log(nowData)
    let data = nowData?.data;
    let processingReportId = nowData?.processingReportId;
    let skuList = [];

    const workshopData = [{ typeName: "成品", productType: 1 }, { typeName: "余料", productType: 2 }, { typeName: "废料", productType: 3 }];
    const workshopList = workshopData.map((v) => {
      return {
        label: v.typeName,
        value: v.productType
      };
    });
    const qualifiedData = [{ typeName: "合格", qualified: 1 }, { typeName: "不合格", qualified: 2 }];
    const qualifiedList = qualifiedData.map((v) => {
      return {
        label: v.typeName,
        value: v.qualified
      };
    });
    const stateData = [{ typeName: "入库", state: 1 }, { typeName: "报废", state: 2 }, { typeName: "返工", state: 3 }];
    const stateList = stateData.map((v) => {
      return {
        label: v.typeName,
        value: v.state
      };
    });


    const productionLineData = await admin.reqPro("warehouse/warehouse/productionLine/list", {});
    const productionLineList = productionLineData.data.map((v) => {
      return {
        label: v.workshopName,
        value: v.id,
        productType: v.productType
      };
    });
    if (nowData.eventName == "detail") {
      admin.setFormDisable("#warehouseProductionOrderForm", true);
      console.log("detail")
      $("#formSubmitUp").hide(); // 隐藏提交按钮
      $("#computerPracticeCode").prop("disabled", true); // 禁用选择上机记录按钮
      form.render();
      console.log(nowData)
    }

    laytpl(warehouseProductionOrderformContent.innerHTML).render(nowData, function (html) {
      $("#formDiv").after(html);
    });

    // 设置 computerPracticeCode 的值
    if (nowData.eventName === "edit" || nowData.eventName === "detail") {
      // form.val("warehouseProductionOrderForm", {
      //     computerPracticeIdName: nowData.data.computerPracticeCode
      // });
      // form.render();
      $("#computerPracticeIdName").html(nowData.data.computerPracticeCode);
      // console.log('12356',nowData.data.computerPracticeCode)
    }


    await admin.initDataSelectPro("production/qualityTesting/admin/qualityTestingType/list", { limit: 99999 }, "qualityTestingTypeId", "类型", async (d, obj) => {
      $("#qualityTestingTypeId").val("");
      if (d.value) {
        $("#qualityTestingTypeId").val(obj.name);
      }

      // 重新渲染 select
      layui.form.render("select");
    });
    await admin.initDataSelectPro("production/qualityTesting/admin/qualityTestingCategory/list", { limit: 99999 }, "qualityTestingCategoryId", "分类", async (d, obj) => {
      $("#qualityTestingCategoryId").val("");
      if (d.value) {
        $("#qualityTestingCategoryId").val(obj.name);
      }
    });
    form.on('select(qualityTestingCategoryId)', function (layData) {
      var elem = layData.elem; // 获得 select 原始 DOM 对象
      var value = layData.value; // 获得被选中的值
      var othis = layData.othis; // 获得 select 元素被替换后的 jQuery 对象
      selectData.qualityTestingCategoryId = value;
      console.log(value)
    });
    form.on('select(qualityTestingTypeId)', function (layData) {
      var elem = layData.elem; // 获得 select 原始 DOM 对象
      var value = layData.value; // 获得被选中的值
      var othis = layData.othis; // 获得 select 元素被替换后的 jQuery 对象
      selectData.qualityTestingTypeId = value;

      console.log(value)
    });
    await admin.initDataSelectPro(
      "warehouse/warehouse/processingReport/list?page=1&limit=999&status=1",
      { limit: 99999 },
      "processingReportId",
      "报工记录编号",
      async (d, obj) => {
        var item = {
          id: "",
          createTime: "",
          materialNumber: "",
          computerPracticeId: "",
          qualityTestingTypeId: selectData.qualityTestingTypeId ? selectData.qualityTestingTypeId : "",
          qualityTestingCategoryId: selectData.qualityTestingCategoryId ? selectData.qualityTestingCategoryId : "",
          processingReportDetailId: "",
          productionPlanCode: "",
          productionOrderNumber: "",
          plannedQuantity: "",
          plannedDeliveryTime: "",
          plannedProductionTime: "",
          customerProductCode: "",
          productSpecCode: "",
          workshopName: "",
          productId: "",
          productionLineName: "",
          productionPlanRemark: "",
          productionproductionuserBundleCodePlanCodePlanCode: "",
          productionuserBundleCodePlanCode: "",
          productProducerName: "",
          productVariety: "",
          productMark: "",
          productBrand: "",
          productMainUnit: "",
          grossWeight: "",
          netWeight: ""
        }
        $("#processingReportDetailId").empty();
        form.val("warehouseProductionOrderForm", item);
        $("#materialDetail").text("无物料");
        $("#materialDetail").attr("title", "无物料");
        console.log(obj);

        // 清空排产信息列表
        table.reload("processPlanFormTable", {
          data: []
        });

        bundleCodeCounter = 1

        if (obj) {
          var itemNow = {};
          Object.keys(obj).forEach((key) => {
            itemNow[key] = obj[key];
          });
          itemNow.createTime = obj.createTime;
          form.val("warehouseProductionOrderForm", itemNow);

        } else {
          form.val("warehouseProductionOrderForm", item);
        }
        itemData = {}
        if (d.value) {
          // 存储数据
          practiceId = d.value
          if (nowData.eventName == "edit") {
            practiceId = nowData.data.computerPracticeId
            console.log(nowData.data, '666666')
          }
          kData = await admin.reqPro("warehouse/warehouse/processingReport/selectProcessingReport", { id: d.value });
          oneItem = kData.data
          console.log(kData)
          itemData = { ...kData.data.warehouseMaterialRequisitionRecordDetail };
          // 遍历 warehouseProcessingReportDetailList，将 remark 设为空字符串
          kData.data.warehouseProcessingReportDetailList.forEach(item => {
            item.remark = "";
            item.inspectedQuantity = item.actualQuantity;
          });

          // 重新加载表格
          table.reload("processPlanFormTable", {
            data: kData.data.warehouseProcessingReportDetailList
          });
          console.log(kData.data.warehouseProcessingReportDetailList)
        } else {
          itemData = {};
          practiceId = 0
        }


        console.log(itemData)

      },
      "code"
    );






    table.on("tool(warehouseMaterialRequisitionRecordFormTable)", function (obj) {
      var d = obj.data; // 获取当前行的数据
      var layEvent = obj.event; // 获取事件名（edit 或 del）
      if (obj.tr) {
        obj.tr.on("click", function (e) {
          e.preventDefault(); // 防止默认行为
        });
      }
      if (layEvent == "del") {
        const index = $(obj.tr[0]).data("index");
        let list = table.cache["warehouseMaterialRequisitionRecordFormTable"].filter((v) => {
          return v["LAY_TABLE_INDEX"] != index;
        });
        table.reload("warehouseMaterialRequisitionRecordFormTable", {
          data: list
        });
      }
    });

    const tableVal = table.render({
      elem: "#processPlanFormTable",
      page: false,
      limit: 999999,
      height: 400,
      data: [

      ],
      cols: [
        [
          { type: "numbers", title: "序号" },
          // {
          //   field: "productType",
          //   title: "报工记录编号",
          //   templet: function(d) {
          //     return admin.setTableSelect("productType", workshopList, nowData.eventName == "detail", "productType")(d);
          //   }
          // },
          { title: "成品规捆包号", field: "finishedProductBundleCode", },
          // { type: "productSpecCode", title: "成品规格", field: "productSpecCode"},
          { type: "inspectedQuantity", title: "质检数量", field: "inspectedQuantity", templet: admin.setTableInput("inspectedQuantity", nowData.eventName == "detail", "inspectedQuantity") },

          // { type: "actualQuantity", title: "实际张数", templet: admin.setTableInput("actualQuantity", nowData.eventName == "detail", "actualQuantity") },
          {
            field: "qualified",
            title: "是否合格",
            templet: function (d) {
              return admin.setTableSelect("qualified", qualifiedList, nowData.eventName == "detail", "qualified")(d);
            }
          },
          { type: "qualityInspectionResults", title: "质检结果描述", templet: admin.setTableInput("qualityInspectionResults", nowData.eventName == "detail", "qualityInspectionResults") },
          { field: "remark", title: "备注信息", templet: admin.setTableInput("remark", nowData.eventName == "detail", "remark") },
          {
            field: "state",
            title: "处置方式",
            templet: function (d) {
              return admin.setTableSelect("state", stateList, nowData.eventName == "detail", "state")(d);
            }
          },
          { title: "操作", toolbar: "#processPlanFormTableBar", hide: nowData.eventName === 'detail' }
        ]
      ],

      done: function (tableData) {
        admin.tableInputChangeByClass("processPlanFormTable", "qualityInspectionResults");
        admin.tableInputChangeByClass("processPlanFormTable", "theoreticalWeight");
        admin.tableInputChangeByClass("processPlanFormTable", "inspectedQuantity");
        admin.tableInputChangeByClass("processPlanFormTable", "actualQuantity");
        admin.tableInputChangeByClass("processPlanFormTable", "remark");
        tableData.data.forEach((v, index) => {
          laydate.render({
            elem: ".plannedProductionTime" + v.LAY_TABLE_INDEX,
            type: "datetime",
            done: function (value, date, endDate) {
              v.plannedProductionTime = value;
            }
          });
        });
      }
    });



    admin.tableSelectChange("processPlanFormTable", "qualified", (index, value, tableData, field, tableId) => {
      // 不再在这里添加新行
      tableVal.reload({
        data: tableData
      });
    });
    admin.tableSelectChange("processPlanFormTable", "state", (index, value, tableData, field, tableId) => {
      // 不再在这里添加新行
      tableVal.reload({
        data: tableData
      });
    });

    if (data && data.id) {
      // await admin.initDataSelectPro(
      //         "warehouse/warehouse/productionOrder/list",
      //         {
      //           customerId: data.customerId
      //         },
      //         "productionOrderNumber",
      //         "生产订单编号",
      //         async (d, obj) => {
      //           $("#productId").empty();
      //           form.val("warehouseProductionOrderForm", { productionOrderNumber: "", workshopName: "" });
      //           if (d.value) {
      //             delete obj.id;
      //             form.val("warehouseProductionOrderForm", { productionOrderNumber: obj.productionOrderNumber });

      //             const { data } = await admin.reqPro("warehouse/warehouse/productionOrder/selectProductionDetail", { id: d.value });
      //             admin.initDataSelect(data.warehouseProductionOrderDetailList, {}, "productId", null, "产品名称", "productCode", "productId");
      //             form.on("select(productId)", function (d) {
      //               let obj = data.warehouseProductionOrderDetailList.find((v) => v.productId == d.value);
      //               delete obj.id;

      //               form.val("warehouseProductionOrderForm", obj);
      //             });
      //           }
      //         },
      //         "productionOrderNumber"
      // );

      // const res = await admin.reqPro("warehouse/warehouse/productionOrder/selectProductionDetail", { id: data.productionOrderNumber });
      form.val("warehouseProductionOrderForm", nowData.data);
      // console.log(nowData);
      // // $("#processingReportId").val(nowData.data.processingReportId);
      // // layui.form.render('select'); // 重新渲染select
      // // form.render('select'); // 重新渲染select
      //   $("#processingReportId").val(nowData.data.processingReportId);
      // console.log(nowData.data.processingReportId);

      // 重新渲染 select
      layui.form.render("select");
      // form.val("warehouseProductionOrderForm", data);
      $("#materialDetail").text(data.materialNumber ? data.materialNumber : "无物料");
      if (nowData.eventName)
        tableVal.reload({
          data: data.warehouseQualityTestingDetailList
        });
      console.log(nowData.data.warehouseQualityTestingDetailList);

    } else {

    }

    if(processingReportId && processingReportId!==''){
      $("#processingReportId").val(processingReportId)
      var item = {
        id: "",
        createTime: "",
        materialNumber: "",
        computerPracticeId: "",
        qualityTestingTypeId: selectData.qualityTestingTypeId ? selectData.qualityTestingTypeId : "",
        qualityTestingCategoryId: selectData.qualityTestingCategoryId ? selectData.qualityTestingCategoryId : "",
        processingReportDetailId: "",
        productionPlanCode: "",
        productionOrderNumber: "",
        plannedQuantity: "",
        plannedDeliveryTime: "",
        plannedProductionTime: "",
        customerProductCode: "",
        productSpecCode: "",
        workshopName: "",
        productId: "",
        productionLineName: "",
        productionPlanRemark: "",
        productionproductionuserBundleCodePlanCodePlanCode: "",
        productionuserBundleCodePlanCode: "",
        productProducerName: "",
        productVariety: "",
        productMark: "",
        productBrand: "",
        productMainUnit: "",
        grossWeight: "",
        netWeight: ""
      }
      const processingReportC = await admin.reqPro("warehouse/warehouse/processingReport/list?page=1&limit=999&status=1", { limit: 99999 });
      let processingReportData=processingReportC.data.filter(i=>i.id==processingReportId)
      bundleCodeCounter = 1
      console.log(processingReportData)
      if(processingReportData.length){
        $("#processingReportCode").val(processingReportData[0].code)
        var itemNow = {};
        let obj=processingReportData[0]
        Object.keys(obj).forEach((key) => {
          itemNow[key] = obj[key];
        });
        itemNow.createTime = obj.createTime;
        form.val("warehouseProductionOrderForm", itemNow);
      }else{
        form.val("warehouseProductionOrderForm", item);
      }
      itemData = {}
      practiceId = processingReportId
      kData = await admin.reqPro("warehouse/warehouse/processingReport/selectProcessingReport", { id: processingReportId });
      oneItem = kData.data
      itemData = { ...kData.data.warehouseMaterialRequisitionRecordDetail };
      // 遍历 warehouseProcessingReportDetailList，将 remark 设为空字符串
      kData.data.warehouseProcessingReportDetailList.forEach(item => {
        item.remark = "";
        item.inspectedQuantity = item.actualQuantity;
      });

      // 重新加载表格
      table.reload("processPlanFormTable", {
        data: kData.data.warehouseProcessingReportDetailList
      });




    }


    //主框确定
    form.on("submit(formSubmit)", function (d) {

      if (!selectData.qualityTestingCategoryId) {
        layer.msg("请选择分类");
        return false;
      }
      if (!selectData.qualityTestingTypeId) {
        layer.msg("请选择类型");
        return false;
      }
      if (!practiceId) {
        layer.msg("请选择记录编号");
        return false;
      }


      delete oneItem.warehouseProcessingReportDetailList;
      oneItem.computerPracticeTime = oneItem.createTime;
      oneItem.processingReportId = practiceId;

      let formData = oneItem;
      formData.warehouseMaterialRequisitionRecordDetailList = table.cache["processPlanFormTable"];

      // if (formData.warehouseMaterialRequisitionRecordDetailList.length === 0) {
      //   layer.msg("必须存在产品信息", { icon: 2, time: 1000 });
      //   return false;
      // }
      // 检查每个 requiredQuantity 是否大于 1
      if (formData.warehouseMaterialRequisitionRecordDetailList.some(item => Number(item.inspectedQuantity) <= 0 || !Number(item.inspectedQuantity))) {
        layer.msg("质检数必须大于0", { icon: 2, time: 1000 });
        return false;
      }

      let addData = formData;

      // if (table.cache["processPlanFormTable"].length == 0) {
      //   layer.msg("请添加数据");
      //   return false;
      // }

      formData.scheduleQuantity = Number(formData.scheduleQuantity);
      formData.remainingProductionSchedule = Number(formData.remainingProductionSchedule);
      formData.productionOrderDetailId = Number(formData.productionOrderDetailId);

      formData.qualityTestingTypeId = selectData.qualityTestingTypeId
      formData.qualityTestingCategoryId = selectData.qualityTestingCategoryId


      formData.warehouseQualityTestingDetailList = table.cache["processPlanFormTable"].map((v) => {
        v.plannedQuantity = Number(v.plannedQuantity);
        v.workshopName = workshopList.find((v1) => v1.value == v.productType)?.label;
        v.qualified = qualifiedList.find((v1) => v1.value == v.qualified)?.value;
        v.state = stateList.find((v1) => v1.value == v.state)?.value;
        v.productionLineName = productionLineList.find((v1) => v1.value == v.productionLineId)?.label;
        return v;
      });
      delete formData.createTime;
      if (formData.warehouseQualityTestingDetailList.some((v) => v.productType == "" && v.productionLineId == "")) {
        layer.msg("请完善表格数据");
        return false;
      }

      // formData.plannedQuantity = formData.warehouseProcessingReportDetailList.reduce((v, c) => v + Number(c.plannedQuantity), 0);

      if (formData.plannedQuantity > formData.scheduleQuantity) {
        layer.confirm("计划生产数量大于需求数，是否继续", { icon: 3, title: "提示" }, function (index) {
          submitForm(formData);
          console.log(formData);
          console.log(oneItem);
        });
      } else {
        submitForm(addData);
      }

      return false;
    });


    function submitForm(formData) {
      console.log(nowData)
      if (!table.cache["processPlanFormTable"]) {
        layer.msg("请添加数据数据");
        return false
      }
      if (nowData.eventName == "add" || nowData.eventName == "processingReportAdd") {
        delete formData.id;
        console.log(formData)
        admin.req(
          "warehouse/warehouse/qualityTesting/add",
          JSON.stringify(formData),
          function (res) {
            layer.closeAll("loading");
            if (res.code == 200) {
              layer.msg(res.msg, { icon: 1 });
              table.reload("ProductionOrderTable");
              layer.closeAll("page");
            } else {
              layer.msg(res.msg, { icon: 2, time: 1000 });
            }
          },
          "POST",
          false,
          "application/json"
        );
      } else if (nowData.eventName == "edit") {
        console.log(formData.warehouseProcessingReportDetailList)
        let editData = nowData.data
        editData.warehouseProcessingReportDetailList = formData.warehouseProcessingReportDetailList
        admin.req(
          "warehouse/warehouse/qualityTesting/updateeQualityTesting",
          JSON.stringify(editData),
          function (res) {
            layer.closeAll("loading");
            if (res.code == 200) {
              layer.msg(res.msg, { icon: 1 });
              table.reload("ProductionOrderTable");
              layer.closeAll("page");
            } else {
              layer.msg(res.msg, { icon: 2, time: 1000 });
            }
          },
          "POST",
          false,
          "application/json"
        );
      }
    }

    table.on("tool(processPlanFormTable)", function (obj) {
      var d = obj.data; // 获取当前行的数据
      var layEvent = obj.event; // 获取事件名（edit 或 del）
      if (obj.tr) {
        obj.tr.on("click", function (e) {
          e.preventDefault(); // 防止默认行为
        });
      }
      if (layEvent == "del") {
        const index = $(obj.tr[0]).data("index");
        let list = table.cache["processPlanFormTable"].filter((v) => {
          return v["LAY_TABLE_INDEX"] != index;
        });
        table.reload("processPlanFormTable", {
          data: list
        });
      }
    });

    $("#Configure").click(function () {
      if ($("#computerPracticeCode").val() === "") {
        layer.msg("请选择上机记录编号");
        return;
      }


      if (table.cache["processPlanFormTable"].some((v) => v.productType == "" && v.productionLineId == "")) {
        layer.msg("请完善表格数据");
      } else {

        let itData = table.cache["processPlanFormTable"][0];
        let codeOne = '';
        if (nowData.eventName === "edit") {
          // 生成 originalBundleCode
          codeOne = itData.originalBundleCode
          itData.productionuserBundleCodePlanCode = itData.originalBundleCode;
          itData.productSpecCode = itData.productSpecCode ? itData.productSpecCode : '无';
          itData.grossWeight = itData.theoreticalWeight;
          // 更新 bundleCodeCounter 为列表中的最大 finishedProductBundleCode 值
          let maxCode = 0;
          table.cache["processPlanFormTable"].forEach(item => {
            const match = item.finishedProductBundleCode.match(/-(\d+)$/);
            if (match) {
              const num = parseInt(match[1], 10);
              if (num > maxCode) {
                maxCode = num;
              }
            }
          });
          bundleCodeCounter = maxCode + 1; // 设置为最大值加一
        }


        if (nowData.eventName !== "edit") {
          itData = itemData
          codeOne = itemData.productionuserBundleCodePlanCode
        }

        const newOriginalBundleCode = codeOne + '-' + bundleCodeCounter++;

        // 创建新的行数据
        const newRow = {
          originalBundleCode: itData.productionuserBundleCodePlanCode,
          finishedProductBundleCode: newOriginalBundleCode,
          qualityInspectionResults: "",
          inspectedQuantity: "",
          productType: "",
          plannedQuantity: "",
          remark: "",
          theoreticalWeight: itData.grossWeight,
          netWeight: oneItem.netWeight,
          productSpecCode: itData.productSpecCode ? itData.productSpecCode : '无'
        };

        // 将新行添加到表格数据中
        table.cache["processPlanFormTable"].push(newRow);

        // 重新加载表格以显示新的数据行
        tableVal.reload({
          data: table.cache["processPlanFormTable"]
        });
      }
    });

    $("#warehouseProductionOrderForm .layui-table-body").css("overflow", "visible");
    $("#warehouseProductionOrderForm .layui-table-box").css("overflow", "visible");
    $("#warehouseProductionOrderForm .layui-table-view").css("overflow", "visible");
  });
</script>

<style>
  #warehouseProductionOrderForm .layui-form-label {
    width: 125px;
  }

  #warehouseProductionOrderForm .layui-input-block {
    margin-left: 155px;
  }

  #warehouseProductionOrderForm .layui-form-item .layui-inline {
    /* margin-bottom: 5px; */
    margin-right: 0px;
  }

  #warehouseProductionOrderForm .layui-table-cell {
    overflow: visible !important;
  }

  .chooseCode {
    position: relative;

    .mask {
      position: absolute;
      width: 100%;
      height: 100%;
      background: rgba(158, 157, 157, 0.1);
      top: 50%;
      left: 50%;
      z-index: 2;
      transform: translate(-50%, -50%);
    }
  }
</style>